package com.gwtcx.sample.serendipity.client.presenter;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtcx.client.CurrentUser;
import com.gwtcx.client.NameTokens;
import com.gwtcx.client.event.LoginAuthenticatedEvent;
import com.gwtcx.client.presenter.SignInPagePresenter;
import com.gwtcx.client.smartgwt.view.handlers.SignInPageUiHandlers;
import com.gwtcx.shared.action.LoginAction;
import com.gwtcx.shared.action.LoginResult;
import com.gwtcx.shared.exception.LoginException;
import com.gwtplatform.dispatch.client.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.NoGatekeeper;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.smartgwt.client.util.SC;

public class SerendipitySignInPagePresenter extends SignInPagePresenter<SerendipitySignInPagePresenter.MyView, SerendipitySignInPagePresenter.MyProxy> implements SignInPageUiHandlers {
	@ProxyStandard
	@NameToken(NameTokens.signInPage)
	@NoGatekeeper
	public interface MyProxy extends Proxy<SerendipitySignInPagePresenter>, Place {
	}
	
	public interface MyView extends View, HasUiHandlers<SignInPageUiHandlers> {
		String getUserNameText();
		String getPasswordText();
		void resetAndFocus();
	}
	
	@Inject
	public SerendipitySignInPagePresenter(EventBus eventBus, MyView view, MyProxy proxy,
			DispatchAsync dispatcher, PlaceManager placeManager) {
		super(eventBus, view, proxy, dispatcher, placeManager);
	
		getView().setUiHandlers(this);
	}
	
	@Override
	protected void onReset() {
		super.onReset();
	
		getView().resetAndFocus();
	}
	
	@Override
	protected void sendCredentialsToServer() {
		String login = getView().getUserNameText();
		String password = getView().getPasswordText();
	
		getDispatcher().execute(new LoginAction(login, password),  new AsyncCallback<LoginResult>() {
			@Override
			public void onFailure(Throwable caught) {
				SC.say(caught.getMessage());
				
				String message = "onFailure() - " + caught.getLocalizedMessage();
		    
				if (caught instanceof LoginException) {
					message = "onFailure() - " + "Invalid User name or Password.";
				}
		      
				getView().resetAndFocus();
			}
			@Override
			public void onSuccess(LoginResult result) {
				SC.say("로그인 성공 : " + result.getSessionKey());
				
				CurrentUser currentUser = new CurrentUser(getView().getUserNameText());
		    
				LoginAuthenticatedEvent.fire(getEventBus(), currentUser);
		    
				PlaceRequest placeRequest = new PlaceRequest(NameTokens.mainPage);
				getPlaceManager().revealPlace(placeRequest);
			}
		});
	}
}
